package com.kltn.zigo;

import java.util.ArrayList;
import java.util.HashMap;

import com.kltn.zigo.data.adapter.CommentUserAdapter;
import com.kltn.zigo.data.adapter.DistrictAdapter;
import com.kltn.zigo.data.loader.CommentUserLoader;
import com.kltn.zigo.data.loader.DistrictLoader;
import com.kltn.zigo.data.loader.UserLoader;
import com.kltn.zigo.process.ImageLoader;

import android.os.AsyncTask;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class ProfileUserActivity extends Activity {
	String userid;
	String username;
	String userdisplayname;
	String useravatarurl;
	
	String useridView;
	String usernameView;
	String userdisplaynameView;
	String useravatarurlView;
	
	TextView tvUsername;
	TextView tvDisplayname;
    ImageLoader imageLoader; 
    ImageView thumb_image;
	ListView listCommentUser;
    
    private MenuItem btnMenu;
    private MenuItem btnSearch;
	
    private ProgressDialog pDialog;
    ArrayList<HashMap<String, String>> commentuserList;
    CommentUserAdapter commentuseradapter;    
    private static LayoutInflater inflater=null;
    HashMap<String, String> userDetailInfo;
    String placeIdClicked = "";
    
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_profile_user);
		
		ActionBar actionBar = getActionBar();		
		actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#b51d21")));
		
		tvUsername = (TextView)findViewById(R.id.tvUsername);
		tvDisplayname = (TextView)findViewById(R.id.tvDisplayname);
		thumb_image = (ImageView)findViewById(R.id.img_Avatar); 
		listCommentUser = (ListView)findViewById(R.id.list_comment_user);
		imageLoader = new ImageLoader(getApplicationContext());
		
		userid = getIntent().getStringExtra("userid");
		username = getIntent().getStringExtra("username");
		userdisplayname = getIntent().getStringExtra("userdisplayname");
		useravatarurl = getIntent().getStringExtra("useravatarurl");
		
		useridView = getIntent().getStringExtra("useridView");
		usernameView = getIntent().getStringExtra("usernameView");
		userdisplaynameView = getIntent().getStringExtra("userdisplaynameView");
		useravatarurlView = getIntent().getStringExtra("useravatarurlView");
		
		
		if(useridView.equals(""))
		{
			useridView = userid;
			usernameView = username;
			userdisplaynameView = userdisplayname;
			useravatarurlView = useravatarurl;
			
			tvUsername.setText(username);
			tvDisplayname.setText(userdisplayname);
			imageLoader.DisplayImage(useravatarurl , thumb_image);
		}
		else
		{
			tvUsername.setText(usernameView);
			tvDisplayname.setText(userdisplaynameView);
			imageLoader.DisplayImage(useravatarurlView , thumb_image);
		}
		//new LoadUser().execute();
		
		new LoadCommentUser().execute();
		
		listCommentUser.setOnItemClickListener(new OnItemClickListener() {
        	@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {							
        		View vi=view;
		        if(view==null)
		            vi = inflater.inflate(R.layout.item_list_place, null);

		        TextView placeID = (TextView)vi.findViewById(R.id.tvPlaceID);
		        
		        placeIdClicked = placeID.getText().toString();
		        
		        Intent i=new Intent(ProfileUserActivity.this,PlaceDetailActivity.class);
		        i.putExtra("placeid", placeIdClicked);
		        i.putExtra("userid",userid);
		        i.putExtra("username", username);
		        i.putExtra("userdisplayname", userdisplayname);
		        i.putExtra("useravatarurl", useravatarurl);
			    startActivity(i);
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.profile_user, menu);
		btnMenu = menu.findItem(R.id.btn_menu);
		btnSearch = menu.findItem(R.id.btn_search);
		
		return true;
	}
	
	@Override
	  public boolean onOptionsItemSelected(MenuItem item) {
		 switch (item.getItemId()) {
		    	
		    case R.id.btn_search:
		    {
		    	Intent i=new Intent(getApplicationContext(),ListPlaceActivity.class);
		    	i.putExtra("userid",userid);
		        i.putExtra("username", username);
		        i.putExtra("userdisplayname", userdisplayname);
		        i.putExtra("useravatarurl", useravatarurl);
			    startActivity(i);
			    break;
		    }
		    
		    case R.id.btn_menu:
		    {
		    	Intent i=new Intent(getApplicationContext(),MainActivity.class);
		        i.putExtra("userid", userid);
		        i.putExtra("username", username);
		        i.putExtra("userdisplayname", userdisplayname);
		        i.putExtra("useravatarurl", useravatarurl);
			    startActivity(i);
		    	break;
		    }
		 }
		 return super.onOptionsItemSelected(item);
	  }
	
class LoadCommentUser extends AsyncTask<String, String, String>{	  
    	
    	CommentUserLoader commentuserloader = new CommentUserLoader(useridView);
	    @Override
	    protected void onPreExecute() {
	    	super.onPreExecute();
            pDialog = new ProgressDialog(ProfileUserActivity.this);
            pDialog.setMessage("Loading comment user. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();	       
	    }

		@Override
		protected String doInBackground(String... args) {
			commentuserList = commentuserloader.GetCommentUser();
			return null;
	    }

	    //*//**
	    // * After completing background task Dismiss the progress dialog
	    // * **//*
	    protected void onPostExecute(String file_url) {
	    	pDialog.dismiss();
			// updating UI from Background Thread
			runOnUiThread(new Runnable() {
				public void run() {
					commentuseradapter = new CommentUserAdapter(ProfileUserActivity.this, commentuserloader, commentuserList);
					//adapter = new PlaceAdapter(ListPlaceActivity.this, placeload);
					listCommentUser.setAdapter(commentuseradapter);		        
			       
				}
			});
	    }
	}

	class LoadUser  extends AsyncTask<String, String, String>{	
		
		UserLoader userloader = new UserLoader(useridView);
	    @Override
	    protected void onPreExecute() {
	    	super.onPreExecute();
	        /*pDialog = new ProgressDialog(PlaceDetailActivity.this);
	        pDialog.setMessage("Loading comment. Please wait...");
	        pDialog.setIndeterminate(false);
	        pDialog.setCancelable(false);
	        pDialog.show();	*/       
	    }
	
		@Override
		protected String doInBackground(String... args) {
			userDetailInfo = userloader.GetUser();
			
			return null;
	    }
	
	    //*//**
	    // * After completing background task Dismiss the progress dialog
	    // * **//*
	    protected void onPostExecute(String file_url) {
	    	//pDialog.dismiss();
			// updating UI from Background Thread
			runOnUiThread(new Runnable() {
				public void run() {	
					usernameView = userDetailInfo.get(userloader.TAG_USER_NAME_ZING);
					userdisplaynameView = userDetailInfo.get(userloader.TAG_USER_DISPLAY_NAME);
					useravatarurlView = userDetailInfo.get(userloader.TAG_USER_AVATAR_ZING);
					
					tvUsername.setText(usernameView);
					tvDisplayname.setText(userdisplaynameView);
					imageLoader.DisplayImage(useravatarurlView , thumb_image);
				}
			});
	    }
	}

}
